import rarfile
import os

def un_rar(file_name,type):
    rar = rarfile.RarFile(file_name)
    new_file = file_name[:-4]
    if os.path.isdir(new_file):
        new_file += f'_{type}'
    rar.extractall(path=new_file)
    rar.close()
    return new_file


def main():
    file_name = input('input file path:\n')

    file_names = [file_name]
    while file_names:
        # 从文件列表中取出一个文件
        file = file_names.pop()
        # 判断文件是否为文件夹
        if os.path.isdir(file):
            file_names.extend([os.path.join(file,f) for f in os.listdir(file)])
        elif file[-4:] == r'.rar':
            file = un_rar(file,'rar')
            file_names.append(file)
        else:
            pass


if __name__ == '__main__':
    main()